import pandas as pd
import numpy as np
import os
import sys
import model_code.gen_data as gd
import model_code.rfg as rfg
import model_code.per_mea as pm
from tensorflow import keras
import tensorflow as tf

source = sys.argv[1] #tongji/vital
#sources = ["tongji","vital"]#针对模型
pre_times = [5,10,15]
ioh_time = "1"
ob_wins = [5,10,15]
#d_path = source + "/dynamic_normalization/" + ioh_time + "-bt.csv"
d_paths = ["tongji/dynamic_normalization/1-bt.csv","vital/dynamic_normalization/1-bt.csv"]
#s_path =  source + "/" + "static/test_case.csv"#测试数据
s_paths =  ["tongji/static/test_case.csv","vital/static/test_case.csv"]#测试数据
c_path = "config_bt.json"

df = pd.DataFrame(columns=["model_path", "model source", "pre_time", "ioh_time", "ob_win", "whether have BT+Static?", "d_path", "s_path", "AUC"
, "precision", "recall", "sensitivity", "specificity", "F1", "accuracy"])
file_path = "test_tongji_result+BT+Static.csv"#存储的文件名
if str(source) == "vital":
    file_path = "test_vital_result+BT+Static.csv"#存储的文件名
content_list = []#待追加的内容列表
i = 1
for d_path,s_path in zip(d_paths,s_paths):
        for pre_time in pre_times:
            pre_time = int(pre_time)
            pre_time = pre_time / 5
            for ob_win in ob_wins:
                ob_win = int(ob_win)
                static, dynamic, label = gd.gen_data(source, d_path, c_path, s_path, pre_time, ob_win)
                dynamic_dim = dynamic.reshape(dynamic.shape[0], dynamic.shape[1], dynamic.shape[2], 1)
                static = static.reshape(static.shape[0], static.shape[1], 1, 1)#reshape静态数据

                model_path = "/home/mount/chy/models_all/BT+Static/" + source + "+BT+Static-" + str(pre_time) + "-" + ioh_time + "-" + str(ob_win) + ".h5"
                
                if not os.path.exists(model_path):
                    print("model is not exist...")
                    os._exit(0)
                model = keras.models.load_model(model_path, custom_objects={"AUC": pm.AUC})

                y_pred = model.predict([static,dynamic_dim, dynamic])
                y_true = label


                with tf.compat.v1.Session() as sess:#获取tensor的值
                    sess.run(tf.compat.v1.global_variables_initializer())
                    AUC = str(sess.run(pm.AUC(y_true, y_pred)))
                    precision =str(sess.run(pm.precision(y_true, y_pred)))
                    recall = str(sess.run(pm.recall(y_true, y_pred)))
                    sensitivity = str(sess.run(pm.TPR(y_true, y_pred)))
                    specificity = str(sess.run(pm.TNR(y_true, y_pred)))  
                    F1 = str(sess.run(pm.F1(y_true, y_pred)))
                    accuracy = str(sess.run(pm.accuracy(y_true, y_pred)))

                print("AUC: " + AUC)
                print("precision: " + precision)
                print("recall: " + recall)
                print("sensitivity: " + sensitivity)
                print("specificity: " + specificity)  
                print("F1: " + F1)
                print("accuracy: " + accuracy)
                print("count:",i)
                i+=1
                #写文件
                content_list.append({"model_path":model_path,"model source":source,"pre_time":str(pre_time*5),"ioh_time":str(ioh_time),
                "whether have BT+Static?":"True","d_path":d_path,"s_path":s_path,"AUC":AUC,"precision":precision,"recall":recall,
                "sensitivity":sensitivity,"specificity":specificity,"F1":F1,"accuracy":accuracy})
                ds = pd.DataFrame(content_list)
                df = df.append(ds, ignore_index=True)
                df.to_csv(file_path, index=False)
                content_list.clear()#一定要清空字典

